# encoding=utf-8
# author:梁子龙
import re

from fastapi.security import OAuth2PasswordBearer
from starlette.requests import Request
from typing import Optional

from config import settings


class MyOAuth2PasswordBearer(OAuth2PasswordBearer):
    """


    """

    def __init__(self, tokenUrl: str, schema: str):
        super.__init__(tokenUrl=tokenUrl,
                       schema_name=schema,
                       scopes=None,
                       description=None,
                       auto_error=True
                       )

        async def __call__(self, request: Request) -> Optional[str]:
            """
            Authorization:
            :param request:
            :return:
            """
            path: str = request.get("path")
            for request_path in settings.WHITE_LIST:
                if re.match(request_path, path):
                    return ""
                else:
                    return super().__call__(request)
